package xjh.him.netty.handler;

import android.os.Message;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import xjh.him.adapter.ChatAdapter;
import xjh.him.common.SharedData;
import xjh.him.entity.ChatMsg;
import xjh.him.netty.dto.ChatMsgDto;


@ChannelHandler.Sharable
public class BaseHandler extends SimpleChannelInboundHandler<Object> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof ChatMsgDto.NotifyMsg) {
            ChatMsgDto.NotifyMsg notifyMsg = (ChatMsgDto.NotifyMsg) msg;
            ChatMsg chatMsg = new ChatMsg();
            chatMsg.setChatId(notifyMsg.getChatId());
            chatMsg.setUserId(notifyMsg.getUserId());
            chatMsg.setType(notifyMsg.getType());
            chatMsg.setContent(notifyMsg.getContent());

            Message msg1 = new Message();
            msg1.what = SharedData.UPDATE_MSG_ITEM_MESSAGE;
            msg1.obj = chatMsg;
            SharedData.updateMsgItemHandler.sendMessage(msg1);

            ChatAdapter chatAdapter = SharedData.CHAT_ADAPTER_MAP.get(chatMsg.getChatId());
            if (chatAdapter != null) {
                Message msg2 = new Message();
                msg2.what = SharedData.UPDATE_CHAT_ITEM_MESSAGE;
                msg2.obj = chatMsg;
                SharedData.updateChatItemHandler.sendMessage(msg2);
            }
        }else {
            SharedData.future.complete(msg);
        }
    }
}
